/*------------------------------------------------------------------------------
 * Copyright 2012 NCR Corporation
 *------------------------------------------------------------------------------
 * All revision information is updated automatically from source code control
 * change records - please do not manually edit.
 *------------------------------------------------------------------------------
 *
 * $LastChangedRevision:: 2123                                     $
 * $LastChangedBy:: rb250096                                       $
 * $LastChangedDate:: 2012-04-18 00:43:25 -0400 (Wed, 18 Apr 2012) $
 */

package com.ncr.nep.dashboardcore.modularization.client;

import java.util.logging.Logger;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.web.bindery.event.shared.HandlerRegistration;
import com.ncr.nep.dashboardcore.mvp.client.Presenter;
import com.ncr.nep.dashboardcore.mvp.client.View;
import com.ncr.nep.dashboardcore.utils.client.AppContext;
import com.ncr.nep.dashboardcore.utils.client.InterWindowEvent;
import com.ncr.nep.dashboardcore.utils.client.InterWindowEventBus;
import com.ncr.nep.dashboardcore.utils.client.InterWindowHandler;

/**
 * Implementation of {@link EntryPoint} that calls its {@link #init()} method
 * to generate an object to automatically add to the root panel.  The returned
 * object can either be a widget or a {@link Presenter}.
 *
 * @author rb250096
 * @version $Revision: 2123 $
 */
public abstract class ModularEntryPoint implements EntryPoint,
        InterWindowHandler {

    //--------------------------------------------------------------------------
    //
    //  Fields
    //
    //--------------------------------------------------------------------------

    /**
     * Logging instance.
     */
    private static Logger log = Logger.getLogger("ModularEntryPoint");

    /**
     * Event bus used to dispatch messages not only within this application
     * or module but between different GWT EntryPoints.
     */
    protected InterWindowEventBus eventBus;

    /**
     * Registration object for bus registration event.
     */
    protected HandlerRegistration busRegistration;

    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------

    @Override
    public void onModuleLoad() {
        log.info("Initializing entry point: " + GWT.getModuleName());
        this.eventBus = new InterWindowEventBus();
        AppContext.setEventBus(this.eventBus);
        this.busRegistration = this.eventBus.addHandlerToSource(
                InterWindowEvent.TYPE,
                InterWindowEvent.REGISTERED_EVENT_TYPE,
                this);

        Object startupObject = this.init();

        IsWidget startupWidget = null;
        if(startupObject instanceof IsWidget) {
            startupWidget = (IsWidget)startupObject;
        }
        else if(startupObject instanceof Presenter) {
            View startupView = ((Presenter)startupObject).getView();

            assert startupView instanceof IsWidget : "Presenter returned " +
                    "init() manage a View that implements IsWidget.";

            startupWidget = (IsWidget)startupView;
        }

        if(startupWidget != null) {
            RootPanel.get().add(startupWidget);
        }

        this.eventBus.init(false);
    }

    /**
     * Initializes the entry point and returns the root view object that
     * will be displayed in the root panel.
     *
     * @return The root view object.  This can either be an
     * {@link IsWidget} instance of a {@link Presenter}.
     */
    public abstract Object init();

    //--------------------------------------------------------------------------
    //
    //  Event handlers
    //
    //--------------------------------------------------------------------------

    @Override
    public void onEventReceived(InterWindowEvent event) {
        this.busRegistration.removeHandler();

        FrameSizeMessage msg = FrameSizeMessage.newMessage();
        msg.setWidth(RootPanel.getBodyElement().getClientWidth());
        msg.setHeight(RootPanel.getBodyElement().getClientHeight());

        log.fine("Window bus registered. sending size. Height: " +
                String.valueOf(msg.getHeight()));

        this.eventBus.fireEvent(new InterWindowEvent(msg, false));
    }
}
